<?php
/* 
 * Copyright (c) 2018-2021   All rights reserved.
 * 
 * 创建时间：2021-10-29 14:31
 *
 * 项目：rm  -  $  - installWidget.php
 *
 * 作者：liwei 
 */

namespace lev\widgets\install;

use Lev;
use lev\base\Widgetv;
use lev\dz\dzDBHelper;
use lev\helpers\dbHelper;
use lev\helpers\ModulesHelper;

!defined('INLEV') && exit('Access Denied LEV');

class installWidget extends Widgetv
{

    public static function run($iden = null)
    {
        //parent::run($show); // TODO: Change the autogenerated stub
        if (empty(Lev::$app['isDiscuz'])) {
            return 'not dz';
        }

        if (!$iden || !($charset = static::ckDzInstallFile($iden))) {
            return '1'.$iden.$charset;
        }

        $mudInfo = ModulesHelper::getModuleInfo($iden);
        if (empty($mudInfo)) {
            return '2';
        }

        $adminurl = Lev::$aliases['@siteurl'].'/admin.php';//ADMINSCRIPT

        return parent::render(0, __DIR__ . '/views/run.php', [
            'insurl'    => $adminurl.'?action=plugins&operation=import&dir='.$iden.'&'.$charset,//installtype=
            'enableurl' => static::getDzEnableurl(),
            'iden'      => $iden,
        ]);
    }


    public static function ckDzInstallFile($iden) {
        if (empty(Lev::$app['isDiscuz'])) {
            return '';
        }

        $idenDir = Lev::$aliases['@modules'] . '/' . $iden;//不支持子模块安装DZ
        $charset = stripos(Lev::$app['charset'], 'gbk') === 0 ? '_SC_GBK' : '_SC_UTF8';
        if (is_file($idenDir . '/discuz_plugin_'.$iden.$charset.'.xml') && is_file($idenDir . '/install.php')) {
            return $charset;
        }
        return false;
    }

    public static function delDzInstallFile($iden) {
        if (empty(Lev::$app['isDiscuz'])) {
            return;
        }
        if (!Lev::isDeveloper($iden)) {
            $idenDir = Lev::$aliases['@modules'] . '/' . $iden;
            is_file($file = $idenDir . '/discuz_plugin_'.$iden.'.xml') && @unlink($file);
            is_file($file = $idenDir . '/discuz_plugin_'.$iden.'_SC_GBK.xml') && @unlink($file);
            is_file($file = $idenDir . '/discuz_plugin_'.$iden.'_SC_UTF8.xml') && @unlink($file);
            is_file($file = $idenDir . '/install.php') && @unlink($file);
            is_file($file = $idenDir . '/upgrade.php') && @unlink($file);
        }
    }

    public static function getDzEnableurl($iden = null) {
        if (empty(Lev::$app['isDiscuz'])) {
            return '';
        }
        if (!$iden) {
            return Lev::toReRoute(['superman/enable-dz-plugin', 'iden'=>$iden, 'id'=>APPVIDEN]);
        }

        static::delDzInstallFile($iden);

        defined('IN_ADMINCP') || define('IN_ADMINCP', TRUE);

        $adminurl = Lev::$aliases['@siteurl'].'/admin.php';//ADMINSCRIPT

        $pluginid = dzDBHelper::getPluginId($iden);
        return $adminurl.'?action=plugins&operation=enable&pluginid='.$pluginid.'&formhash='.formhash();
    }

    public static function dzcofigurl($iden) {
        if (empty(Lev::$app['isDiscuz'])) {
            return '';
        }
        $adminurl = Lev::$aliases['@siteurl'].'/admin.php';//ADMINSCRIPT

        $pluginid = dzDBHelper::getPluginId($iden);
        return $adminurl . '?action=plugins&operation=config&do='.$pluginid;
    }

    public static function undz($iden)
    {
        if (empty(Lev::$app['isDiscuz'])) {
            return '';
        }
        $adminurl = Lev::$aliases['@siteurl'].'/admin.php';//ADMINSCRIPT

        $pluginid = dzDBHelper::getPluginId($iden);

        return !$pluginid ? '' : [
            '检测到DZ插件配置未卸载【点我卸载】'=> $adminurl.'?action=plugins&operation=delete&pluginid='.$pluginid
        ];
    }

}